<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Expert_admin_module extends CI_Module {
    private   $data ;
    private   $sid ;
    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->load->model('expert.Expert_model');
        $this->load->model('member.User_model');
        $this->load->model('admin.Upload_model');
        $this->load->model('admin.Adminuser_model');
    }

    public function edit_expert()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $array = $this->input->post('Form');
            $card_info = $this->input->post('Card');
            $id = $this->input->post('id');
            if(!empty($id))
            {
                $where = array('id'=>$id);
                $array['id'] = $id ;
                $this->Adminuser_model->edit_row($card_info,$where);
                $res = $this->Expert_model->_edit_expert($array,$where) ;

            }
            $backurl = site_url('expert/admin/edit_expert/'.$id);
            if($res)
            {
                $this->success('操作成功',$backurl,true);
            }
            else
            {
                $this->error('操作失败',$backurl,true);
            }
        }

        $param = $this->uri->segment(4);
        if(!empty($param))
        {
            $id = $param ;
        }
        else
        {
            $id = $this->session->userdata('admin_user_id');
        }
        $expert_info = $this->Expert_model->_expert_info($id);
        $this->data = $expert_info ;
        $this->data['card_info'] = $this->Adminuser_model->get_row(array('id'=>$id),'bank,bank_info,card_name,card_number');
        $this->data['select'] = $this->Expert_model->_expert_select();
        $this->data['form_url'] = site_url('/expert/admin/edit_expert/');
        $this->data['id'] = $id ;
        $this->load->view('admin/view_edit_expert',$this->data);
    }



    public function add_journey()
    {
        if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
        {
            $array = $this->input->get_post('Form');
            $array['sid'] = $this->sid ;
            $admin_user_id = $this->session->userdata('admin_user_id');
            $res = $this->Expert_model->add_journey($array,$admin_user_id);
            if($res)
            {
                $this->success('操作成功',site_url('expert/admin/journey_list'),true);
            }
            else
            {
                $this->error('操作失败',site_url('expert/admin/add_journey'),true);
            }
        }
        $this->data['form_url'] = site_url('/expert/admin/add_journey/');
        $this->load->view('admin/view_add_journey',$this->data);
    }

    public function delete_journey()
    {
        $id = $this->input->get('id');
        if(empty($id))
        {
            $post_id = $this->input->post('id');
            $res = $this->Expert_model->_delete_journey("id in ($post_id)");
        }
        else
        {
            $res = $this->Expert_model->_delete_journey(array('id'=>$id));

        }
        if($res)
        {
            $this->success('操作成功',site_url('expert/admin/journey_list'),true);
        }
        else
        {
            $this->error('操作失败','',true);
        }
    }

    public function expert_list()
    {
        $sid = $this->sid ;
        $e = $this->db->dbprefix('expert');
        $page_config['perpage']=20;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='expert/admin/expert_list';//url
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $where = array($e.'.sid'=>$sid);
        $result = $this->Expert_model->_list($where,$page_config['perpage'],$page_config['nowindex'],'1');
        $page_config['total'] = $this->Expert_model->get_expert_count(array(),'1');
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $this->data['expert_array'] = $result['list'] ;
        $this->load->view('/admin/view_expert_list',$this->data);
    }


    public function journey_list()
    {
        $user_id = $this->session->userdata('admin_user_id');
        $page_config['perpage']=5;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='expert/admin/journey_list';//url
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Expert_model->get_journey_count(array('user_id'=>$user_id));
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $journey_array = $this->Expert_model->get_journey_list(array('user_id'=>$user_id),$page_config['perpage'],$page_config['nowindex']);
        $this->data['journey_array'] = $journey_array ;
        $this->load->view('/admin/view_journey_list',$this->data);
    }


    /**
     * 达人状态
     *
     *
     */

    public function expert_status()
    {
        $post = $this->input->post();
        $data[$post['field']] = $post['val'];
        if($post['field']=='is_open')
        {
            $res = $this->Expert_model->_edit_expert_status($data,array('id'=>$post['id']));
        }
        if($post['field']=='is_recommend')
        {
            $res = $this->Expert_model->_edit_expert_recommend($data,array('id'=>$post['id']));
        }
        if($post['field']=='is_guide')
        {
            $res = $this->Expert_model->_edit_expert_recommend($data,array('id'=>$post['id']));
        }
        if($res)
        {
            $this->success('操作成功','',true);
        }
        else
        {
            $this->error('操作失败','',true);
        }
    }


    public function customer_relation()
    {
        $expert_id = $this->input->get_post('expert_id');
        $user_id   = $this->input->get_post('user_id');
        if(!empty($expert_id)&&!empty($user_id))
        {
            $res = $this->Expert_model->_expert_customer($user_id,$expert_id);
            return $res ;
        }
        else
        {
            return array('status'=>'0','info'=>'无效参数','msg'=>'invalid_param');
        }


    }


    public function customer_list()
    {
        $expert_id = '1' ;
        $page_config['perpage']=5;   //每页条数
        $page_config['part']=2;//当前页前后链接数量
        $page_config['url']='expert/admin/customer_list'; //url
        $page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
        $page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
        $page_config['total'] = $this->Expert_model->get_customer_count($expert_id);
        $this->load->library('mypageclass');
        $this->mypageclass->initialize($page_config);
        $customer_array = $this->Expert_model->get_customer_list($page_config['perpage'],$page_config['nowindex'],$expert_id);
        $this->data['customer_array'] = $customer_array ;
        $this->load->view('/admin/view_customer_list',$this->data);
    }



    public function upload_avatar()
    {
        $id = $this->input->get('id');
        $config = $this->Upload_model->upload_config('expert/avatar');
        $this->load->library('upload',$config);
        $absolute_path = str_replace('\\', '/', getcwd()).'/';
        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->error($error['error'],'',true);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $message = str_replace($absolute_path, '', $data['upload_data']['full_path']);
            $file_ext = $data['upload_data']['file_ext'] ;
            $dir_url = '/uploads/'.$GLOBALS['sid'].'/expert/avatar/' ;
            dmkdir($dir_url);
            $des_url = $dir_url.$id.$file_ext ;
            $des = getcwd().$des_url;
            if(copy($message,$des))
            {
                if(unlink($message))
                {
                    $new_url = substr($des_url,1);
                    $where = array('id'=>$id);
                    $array['avatar'] = $new_url ;
                    $res = $this->Expert_model->_edit_expert($array,$where) ;
                }
            }

            
           
            $this->success(URLPIC.$new_url,'',true);
        }

    }


    public function upload_appearance()
    {

        $id = $this->input->get('id');
        $config = $this->Upload_model->upload_config('expert/appearance');
        $this->load->library('upload',$config);
        $absolute_path = str_replace('\\', '/', getcwd()).'/';
        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->error($error['error'],'',true);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $message = str_replace($absolute_path, '', $data['upload_data']['full_path']);
            $file_ext = $data['upload_data']['file_ext'] ;
            $dir_url = '/uploads/'.$GLOBALS['sid'].'/expert/appearance/' ;
            $des_url = $dir_url.$id.$file_ext ;
            $des = getcwd().$des_url;
            if(copy($message,$des))
            {
                if(unlink($message))
                {
                    $new_url = substr($des_url,1);
                    $where = array('id'=>$id);
                    $array['appearance'] = $new_url ;
                    $res = $this->Expert_model->_edit_expert($array,$where) ;
                }
            }

            
           
            $this->success(URLPIC.$new_url,'',true);
        }
    }
    public function upload_guide()
    {

        $id = $this->input->get('id');
        $config = $this->Upload_model->upload_config('expert/front/guide');
        $this->load->library('upload',$config);
        $absolute_path = str_replace('\\', '/', getcwd()).'/';
        if ( ! $this->upload->do_upload('file'))
        {
            $error = array('error' => $this->upload->display_errors());
            $this->error($error['error'],'',true);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $message = str_replace($absolute_path, '', $data['upload_data']['full_path']);
            $file_ext = $data['upload_data']['file_ext'] ;
            $dir_url = '/uploads/'.$GLOBALS['sid'].'/expert/front/guide/' ;
            $des_url = $dir_url.$id.$file_ext ;
            $des = getcwd().$des_url;
            if(copy($message,$des))
            {
                if(unlink($message))
                {
                    $new_url = substr($des_url,1);
                    $where = array('id'=>$id);
                    $array['guide'] = $new_url ;
                    $res = $this->Expert_model->_edit_expert($array,$where) ;
                }
            }

            
           
            $this->success(URLPIC.$new_url,'',true);
        }
    }


}
